package com.helloniu.wb.tools {

import com.helloniu.data.wb.LineStyle;
import com.helloniu.data.wb.WBColor;
import com.helloniu.wb.CursorManager2;
import com.helloniu.wb.containers.ShapeTypeDef;

import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;

public class LineTool extends DrawingTool {
    private var resizeBox:LineResizeBox;

    //CONSTRUCTOR
    public function LineTool() {
        super();
        
        
        this.resetDefaultWBStyle()
        this.setShapeType(ShapeTypeDef.LINE);
    }
    
    override protected function resetDefaultWBStyle():void {
    	this.setWBStyle(new LineStyle(), false);
    }

    override public function setEndPoint(endP:Point, appendEnd:Boolean = true):void {
        shapeRawData[1] = endP;        
    }

    //OVERRIDDEN FUNCTIONS
    override protected function createChildren():void {
        super.createChildren();

        resizeBox = new LineResizeBox();
        resizeBox.visible = false;
        this.addChild(resizeBox);

        resizeBox.addEventListener(MouseEvent.MOUSE_DOWN, resizeMouseDownHandler);
    }

    public function resizeMouseDownHandler(event:MouseEvent):void {
        //currentResizeHandle = event.target as ResizeHandle;
        //stop propogation so the BlankPaperWrapper doesn't listen and
        //act on the mouseDown event
        event.stopPropagation();

        //trace('MOUSE_DOWN - DrawingTool');
        //lc = getStyle('mouseDownColor');

        //adjust our move x and y's
        moveStartX = event.target.parent.contentMouseX;
        moveStartY = event.target.parent.contentMouseY;

        moveEndX = event.target.parent.contentMouseX;
        moveEndY = event.target.parent.contentMouseY;
        moveDistanceX = 0;
        moveDistanceY = 0;

        startDragging(event);
        //invalidateDisplayList();
    }

    override protected function resetMoveVars():void{
        if(resizeBox.getResizeType() == 0) {
            addStartXY(moveDistanceX, moveDistanceY);
        }
        else if(resizeBox.getResizeType() == 1) {
            addEndXY(moveDistanceX, moveDistanceY);
        }
        else {
            addStartXY(moveDistanceX, moveDistanceY);
            addEndXY(moveDistanceX, moveDistanceY);
        }

        //Tool.traceT("--move-or-modify---line: " + isDragging +"|"+ isDragging2);
    }

    override protected function repaint():void {
        super.repaint();

        graphics.clear();

        if(!isValidLine()) {
            return;
        }

        drawLine();
        var rboxvis:Boolean = CursorManager2.getToolType() == CursorManager2.MODIFY_TOOL;
        showResizeBox(rboxvis);
    }

    private function showResizeBox(vis:Boolean = true):void {
        if(!vis) {
            ResizeManager.setResizeBox(resizeBox, false);
            return;
        }

        var shapeDots:Array = getShapePoints();
        var sp:Point = shapeDots[0] as Point;
        var ep:Point = shapeDots[1] as Point;

        resizeBox.setStartPoint(sp);
        resizeBox.setEndPoint(ep);
        ResizeManager.setResizeBox(resizeBox);
        resizeBox.invalidateDisplayList();
    }

    private function isValidLine():Boolean {
        var shapeDots:Array = getShapePoints();
        var sp:Point = shapeDots[0] as Point;
        var ep:Point = shapeDots[1] as Point;

        // @by tonyy, do not add one point
        if(Point.distance(sp, ep) < 10) {
            return false;
        }

        return true;
    }

    override protected function updateDisplayList(w:Number, h:Number):void {
        graphics.clear();

        if(!isValidLine()) {
            return;
        }

        drawLine();
        var rboxvis:Boolean = CursorManager2.getToolType() == CursorManager2.MODIFY_TOOL;
        showResizeBox(rboxvis);
    }

    override protected function isDTOutofWB():Boolean {
        if(super.isDTOutofWB()) {
            return true;
        }

        var prect:Rectangle = new Rectangle(0, 0, this.parent.width, this.parent.height);

        var shapeDots:Array = getShapePoints();
        var sp:Point = shapeDots[0] as Point;
        var ep:Point = shapeDots[1] as Point;

        if(sp != null && !prect.containsPoint(sp)) {
            return true;
        }

        if(ep != null && !prect.containsPoint(ep)) {
            return true;
        }

        return false;
    }
    
	// paint the shape use the shape info
    private function drawLine():void {
        if(!isValidLine()) {
            return;
        }

		// update shape info
		if(getCurrWBStyle() is LineStyle) {
			setWBStyle(getCurrWBStyle());
		}

        var shapeDots:Array = getShapePoints();
        var sp:Point = shapeDots[0] as Point;
        var ep:Point = shapeDots[1] as Point;

		var lineColor:WBColor = (getWBStyle() as LineStyle).lineColor;
		var lineWidth:uint = (getWBStyle() as LineStyle).lineWidth;		
        graphics.lineStyle(lineWidth, lineColor.color, lineColor.alpha);

        graphics.moveTo(sp.x, sp.y);
        graphics.lineTo(ep.x, ep.y);
    }    
}
}